//
//  uMengService.m
//  uMeng
//
//  Created by ZhuoLaiQiang@Gmail.com on 6/14/11.
//  Copyright 2011 Mtime. All rights reserved.
//

#import "uMengService.h"
#import "ASIFormDataRequest.h"
#import "CONSTS.h"
#import "uMengAppDelegate.h"


@implementation uMengService

//简单的得到html代码
+ (NSString*)getHTMLByUrl:(NSURL*)url
{
    NSError *error = nil;
    NSString *html =[[NSString alloc] initWithContentsOfURL: url encoding: NSUTF8StringEncoding error: &error];
    
    if (!error)
    {
        return [html autorelease];
    }
    
    return nil;
}

+ (NSString*)logOnByEmail:(NSMutableDictionary*)userInfo
{
    NSURL *url = [[NSURL alloc] initWithString:logOnUrl];
	NSString *result = [self requestUrl:url WithParameters: userInfo];
	[url release];
    //	NSLog(@"--------------------------------loginMtimeByEmail:%@",[result description]);
	return result;

    
    
    /*
    
    NSURL *url = [[NSURL alloc] initWithString: logOnUrl];
    NSError *error;
    NSString *html =[NSString stringWithContentsOfURL: url encoding: NSUTF8StringEncoding error: &error];
    TTDINFO(@"%@", html);
	NSString *result = [self requestUrl:url WithParameters: userInfo];
	[url release];
	return result;
    
    
    NSError *error;
    NSURL *url = [NSURL URLWithString:@"https://blog.prosight.me/"];
    NSString *content = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding error:&error];
*/
}

@end

@implementation uMengService(Private)
+ (NSString*) requestUrl:(NSURL*)url WithParameters:(NSMutableDictionary*)params 
{
	return [uMengService requestUrl:url WithParameters:params ignoreIndicator: YES];
}

+ (NSString*) requestUrl:(NSURL*)url WithParameters:(NSMutableDictionary*)params ignoreIndicator:(BOOL)ignore
{
    
    uMengAppDelegate *app = (uMengAppDelegate *)[[UIApplication sharedApplication] delegate];
	if (!ignore) {
		id objYES = [[NSObject alloc] init];
		[app performSelectorOnMainThread:@selector(showGlobalIndicator:) withObject:objYES waitUntilDone:YES];
		[objYES release];
	}
    
    NSString *result = nil;
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	//request.stringEncoding = NSUnicodeStringEncoding;
    NSString *postData = @"";
	if (params != nil) {
		NSUInteger count = [[params allKeys] count];
        NSString *value = @"";
		for (NSUInteger i=0; i<count; i++) {
			id key = [[params allKeys] objectAtIndex:i];
            value = (NSString *)[params objectForKey:key];
            [request setPostValue:value forKey:key];
#warning 
            value = [request unicodeEncodeURL:value];
            postData = [postData stringByAppendingFormat:@"%@=%@",key,value];
            if( i<count-1 ){
                postData = [postData stringByAppendingString:@"&"];
            }
		}
        //NSLog(@"postData: %@",postData);
	}

    //NSLog(@"requestHeaders: %@",request.requestHeaders);
	
	[request startSynchronous];
	NSError *error = [request error];
	NSLog(@"error: %@",error);
	if (!error) {
		result = [request responseString];
		TTDINFO(@"raw response string : %@",result);
		//NSData *jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
		//result = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];
	}else {
		result = @"error";
	}
    
	if (!ignore) {
		[app performSelectorOnMainThread:@selector(showGlobalIndicator:) withObject:nil waitUntilDone:YES];
	}
	
	return result;
}

@end